package cn.yhbk.web.modules.manager.Controller;

import cn.yhbk.web.common.api.CommonResult;
import cn.yhbk.web.modules.manager.Pojo.VO.GpaFlowPath.FlowPathGet;
import cn.yhbk.web.modules.manager.Pojo.VO.GpaFlowPath.FlowPathUpdate;
import cn.yhbk.web.modules.manager.Service.ITbGpaCheckService;
import cn.yhbk.web.modules.manager.Service.ITbGpaFlowPathService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 绩点评定顺序 前端控制器
 * </p>
 *
 * @author 有何不可
 * @since 2023-08-05
 */
@RestController
@RequestMapping("/tbGpaFlowPath")
@Api(tags = "绩点评定顺序管理")
public class TbGpaFlowPathController {

    @Autowired
    ITbGpaFlowPathService flowPathService;

    @Autowired
    ITbGpaCheckService checkService;


    @ApiOperation(value = "修改评定顺序",notes = "如果有正在评定得记录，则不能修改")
    @PostMapping("/manage")
    public CommonResult updateGpaFlowPath(@Valid @RequestBody FlowPathUpdate update){
        //判断order是否符合条件，需要保持order得连续性
        boolean enable = flowPathService.checkOrder(update.getOrder());
        if(!enable)return CommonResult.failed("order不符合条件");
        //判断是否有正在评定得记录
        boolean exist = checkService.haveChecking();
        if(exist)return CommonResult.failed("有正在审核的申请，不能修改!");

        //修改
        boolean success = flowPathService.updateFlowPath(update);
        if(success)return CommonResult.success("修改成功!");
        return CommonResult.failed("修改失败!");
    }

    @ApiOperation("获取评定顺序")
    @GetMapping("/common")
    public CommonResult getGpaFlowPath(){
        List<FlowPathGet> list = flowPathService.getFlowPath();

        return CommonResult.success(list);
    }

    @ApiOperation("删除")
    @DeleteMapping("/manage/{order}")
    public CommonResult deleteGpaFlowPath(@PathVariable Integer order){

        //判断order是否符合条件，需要保持order得连续性
        boolean enable = flowPathService.checkOrder(order);
        if(!enable)return CommonResult.failed("order不符合条件");

        //判断是否有正在评定得记录

        boolean success = flowPathService.deleteGpaFlowPath(order);
        if(success)return CommonResult.success("删除成功!");

        return CommonResult.failed("删除失败!");
    }
}
